﻿using System;
using System.Linq;
using Windows.UI.ApplicationSettings;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;

// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236

namespace MDT_Metro_monitoring
{
    public sealed partial class ServerManagementSettings : UserControl
    {
        private Server toBeModified;

        public ServerManagementSettings()
        {
            this.InitializeComponent();

            if (App.Servers != null)
            {
                var result = from server in (from srv in App.Servers orderby srv.Online descending select srv) group server by true into grp select grp;
                serverSource.Source = result;
            }
            removeButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }


        public void GoBack(object sender, RoutedEventArgs e)
        {
            if (this.Parent.GetType() == typeof(Popup))
            {
                ((Popup)this.Parent).IsOpen = false;
            }
            App.updateAvailable = true;
            SettingsPane.Show();
        }

        private void serverItemclicked(object sender, ItemClickEventArgs e)
        {
            ipName.Text = ((Server)e.ClickedItem).Ipaddress;
            port.Text = ((Server)e.ClickedItem).Port;
            toBeModified = (Server)e.ClickedItem;
            addButton.Content = "Replace";
            removeButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
        }

        private void removeSrvClicked(object sender, RoutedEventArgs e)
        {
            if (srvListGridView.SelectedItems != null)
            {
                foreach (Server s in srvListGridView.SelectedItems){
                    App.removeServer(s);
                }
                var result = from server in (from srv in App.Servers orderby srv.Online descending select srv) group server by true into grp select grp;
                serverSource.Source = result;
                removeButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
        }

        private void addSrvClicked(object sender, RoutedEventArgs e)
        {
            if (toBeModified != null)
            {
                int index = App.Servers.IndexOf(toBeModified);
                App.Servers.ElementAt(index).Id = ipName.Text;
                App.Servers.ElementAt(index).Port = port.Text;
                toBeModified = null;
                addButton.Content = "Add";
            }
            else
            {
                Server s = null;
                try
                {
                    s = new Server(ipName.Text, port.Text);
                    App.addServer(s);
                }
                catch (Exception)
                {
                    //parent.Error(exception.Message);
                }
                
            }
            if (App.Servers != null)
            {
                var result = from server in (from srv in App.Servers orderby srv.Online descending select srv) group server by true into grp select grp;
                serverSource.Source = result;
            }
        }

        private void srvListGridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (srvListGridView.SelectedItems.Count != 0)
            {
                removeButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
                if (srvListGridView.SelectedItems.Count == 1)
                {
                    addButton.Content = "Replace";
                    ipName.Text = ((Server)srvListGridView.SelectedItem).Ipaddress;
                    port.Text = ((Server)srvListGridView.SelectedItem).Port;
                    toBeModified = (Server)srvListGridView.SelectedItem;
                    addButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
                }
                else
                {
                    addButton.Content = "Add";
                    toBeModified = null;
                    addButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                }
            }
            else
            {
                removeButton.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
                addButton.Content = "Add";
                toBeModified = null;
                addButton.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
        }
    }
}
